package com.project.JunM.controller;

import com.project.JunM.entity.SCell;
import com.project.JunM.service.SCellService;
import com.project.common.entity.PageVo;
import com.project.common.entity.SysUsers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

@RestController
@RequestMapping("/sCell")
public class SCellController {

    @Autowired
    private SCellService sCellService;

    /*安全库存配置单复核*/
    @PostMapping("/querySearchProductIdAll.action")
    public PageVo<SCell> SearchProductIdAll(@RequestBody Map<String,String> map){

        PageVo<SCell> pageVo = sCellService.SearchProductIdAll(map);
        System.out.println();
        return pageVo;
    }


    @PostMapping("/addSCell")
    public void addSCell(SCell sCell, HttpServletRequest request) throws IOException {
        request.setCharacterEncoding("UTF-8");
        //获取当前最大存储量
        Integer amount = sCell.getAmount();
        //给当前存储量 赋初值
        sCell.setAmount(amount);
        SysUsers user = (SysUsers) request.getSession().getAttribute("user");
        sCell.setRegister(user.getLoginId());

        sCellService.insertSelective(sCell);
    }

    /*安全库存配置查询*/
    @PostMapping("/queryConfigurationAll.action")
    public PageVo<SCell> queryConfigurationAll(@RequestBody Map<String,String> map){

        PageVo<SCell> pageVo = sCellService.queryConfigurationAll(map);

        return pageVo;
    }

    //安全库存配置变更
    /*安全库存配置查询*/
    @PostMapping("/queryChangeProductIdAll.action")
    public PageVo<SCell> queryChangeProductIdAll(@RequestBody Map<String,String> map){

        PageVo<SCell> pageVo = sCellService.queryChangeProductIdAll(map);

        return pageVo;
    }

    @PostMapping("/updateSCellByProductId")
    public void updateSCellByProductId(SCell sCell, HttpServletRequest request,Integer flag) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        sCellService.updateSCellByProductId(sCell,flag);
    }

    @PostMapping("/SearchDFileByProductId")
    public SCell SearchDFileByProductId(String productId){
        SCell sCell = sCellService.SearchDFileByProductId(productId);
        return sCell;
    }

    //检查库存中是否有 数据
    @PostMapping("/querySCellByProduct.action")
    public int querySCellByProduct(String productId){
        SCell sCell = sCellService.querySCellByProduct(productId);
        if(sCell==null){
            return 0;
        }else {
            return 1;
        }
    }

    //检查库存中是否有 数据
    @PostMapping("/querySCellByProduct1.action")
    public Integer querySCellByProduct1(String productId){
        SCell sCell = sCellService.querySCellByProduct(productId);

        Integer amount = sCell.getAmount();
        System.out.println(amount);
        return amount==null?0:amount;

    }
}
